/**
 * Encrypt class
 * 
 * @author Kodejava.org
 * @url http://www.kodejava.org/examples/122.html
 */

package helpers;

import java.security.MessageDigest;

public class Encrypt
{
	
	public Encrypt(){}
	
	public String encrypt(char[] charPassword)
	{
		String algorithm = "sha-256";
		String password = "";
		
		for(int i = 0; i < charPassword.length; i++)
		{
			password += charPassword[i];
		}
		
		byte[] plainText = password.getBytes();

		MessageDigest md = null;

		try {		
			md = MessageDigest.getInstance(algorithm);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		md.reset();		
		md.update(plainText);
		byte[] encodedPassword = md.digest();

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < encodedPassword.length; i++) {
			if ((encodedPassword[i] & 0xff) < 0x10) {
				sb.append("0");
			}

			sb.append(Long.toString(encodedPassword[i] & 0xff, 16));
		}

		return sb.toString(); // password
	}
}